<?php
ini_set('display_errors', 'on');
error_reporting(E_ERROR);
//error_reporting(E_ALL);

require_once(CONFIG_DIR . 'reg_product_config.php');
require_once(LIB_DIR . 'api/general.php');

$urlShop = "http://rate.taobao.com/feedRateList.htm?userNumId=SELLER_ID&auctionNumId=PRODUCT_ID&currentPageNum=1";
$urlMall = "http://rate.tmall.com/list_detail_rate.htm?itemId=PRODUCT_ID&sellerId=SELLER_ID";

$shopDAO = GetShopDAO();
$productDAO = GetProductDAO();
$reviewDAO = GetReviewDAO();
$taskMgr = GetTaskManager();
$errmsg = GetMessageStack();

if (isset($_GET['pid'])) {
    $pid = intval($_GET['pid']);
    if ($pid <= 0) {
        edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));
    }
} else {
    edp_redirect(edp_href_link(PAGENAME_PAGE_NOT_FOUND));
}

$productTaobaoInfo = $productDAO->GetProductInfoPlus( 'taobao', $pid );
$productTaobaoId = $productTaobaoInfo['taobao_id'];

if( isset($productTaobaoId)){
    // 主逻辑
    $productInfo=$productDAO->GetProductInfo2($pid);
    $sid = $productInfo['shop_id'];
    $shopInfoTaobao = $shopDAO->GetShopInfoTaobao($sid);
    require_once(LIB_DIR . 'api/taobao/Taoapi.php');

    // get taobao seller_id
    $Taoapi = new Taoapi;
    $Taoapi->method = 'taobao.taobaoke.shops.convert';
    $Taoapi->fields = 'user_id,shop_title,click_url,commission_rate';
    $Taoapi->sids = $shopInfoTaobao['taobao_id'];
    $Taoapi->pid = TAOBAOKE_ID;
    $TaobaokeData = $Taoapi->Send('get', 'xml')->getArrayData();

    // get seller_id from taobaoke api
    if (edp_is_empty_string($Taoapi->getErrorInfo()) && !empty($TaobaokeData)) {
        // get all taobaoke product
        $seller_id = $TaobaokeData['taobaoke_shops']['taobaoke_shop']['user_id'];

        if( $shopInfoTaobao['is_mall'] == 0 ){
            $url = str_replace( "SELLER_ID", $seller_id, $urlShop );
            $url = str_replace( "PRODUCT_ID", $productTaobaoId, $url );
        }
        else{
            $url = str_replace( "SELLER_ID", $seller_id, $urlMall );
            $url = str_replace( "PRODUCT_ID", $productTaobaoId, $url );
        }

        $snoopy = new Snoopy;
        $snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
        $snoopy->fetch($url);
        $html_content = $snoopy->results;
        if( $shopInfoTaobao['is_mall'] == 0 ){
            $html_content = str_replace("(", "", $html_content);
            $html_content = str_replace(")", "", $html_content);
        }
        else{
            $html_content = '{'.$html_content .'}';
        }
        $html_content = GBKtoUTF8( $html_content );
        $strReviews = json_decode( $html_content, true );

        //普通淘宝店铺
        if($shopInfoTaobao['is_mall'] == 0){
            if ( isset($strReviews['comments']) && count($strReviews['comments']) > 0 ) {
                foreach ($strReviews['comments'] as $review) {
                    // check review exist or not
                    $isExist = $reviewDAO->CheckReviewExist( $review['rateId'] );
                    if( $isExist !== false ){
                        echo "Review ".$review['content']." alread exist<br />";
                        continue;
                    }

		    if(  strlen($review['content']) < 10 ) continue;

                    // taobao rate 1 好评， 0 中评， -1 差评
                    if( $review['rate'] == 1 ){
                        $score = 3;
                        $reality = 3;
                        $service = 3;
                        $velocity = 3;
                        $rate = 3;
                    }
                    else if( $review['rate'] == 0 ){
                        $score = 1;
                        $reality = 1;
                        $service = 1;
                        $velocity = 1;
                        $rate = 1;
                    }
                    else if( $review['rate'] == 0 ){
                        $score = 0;
                        $reality = 0;
                        $service = 0;
                        $velocity = 0;
                        $rate = 0;
                    }
                    $product_id_list = $pid;
                    $product_id = $pid;
                    $content = $review['content'];
                    $paymethod = "";
                    $feature = "";
                    $tag = "";
                    $view_count = 0;
                    $flower_count = 0;
                    $egg_count = 0;
                    $response_count = 0;
                    $pic_count = 0;
                    $product_count = 1;
                    $uploaded_files = "";
                    $reviewDAO->AddReview(1, $sid, GetCurrentDate(), $product_id_list,
                        $score, $reality, $service, $velocity, $rate, $content, $paymethod,
                        $feature, $tag, $view_count, $flower_count, $egg_count, $response_count,
                        $pic_count, $product_count, $uploaded_files, $product_id, $review['rateId'] );

                    //inset task to Message Queue
                    $taskMgr->AddProductScoreTask( $pid );
                    $taskMgr->AddShopScoreTask( $sid );
                    $data = array( "shop_id"=> $sid, "tag"=>"", "paymethod"=>"", "feature"=>"");
                    $taskMgr->AddShopInfoTask( $data );
                }
            } else {
                $errmsg->add('reg', '获取商品评价失败！');
                TransferTo('not_allow_add_review');
                return;
            }
        }
        // 淘宝商城
        else{
            if ( isset($strReviews['rateDetail']['rateList']) && count($strReviews['rateDetail']['rateList']) > 0 ) {
                foreach ($strReviews['rateDetail']['rateList'] as $review) {
                    // check review exist or not
                    $isExist = $reviewDAO->CheckReviewExist( $review['id'] );
                    if( $isExist !== false ){
                        echo "Review ".$review['rateContent']." alread exist<br />";
                        continue;
                    }
		    if( $review['rateContent'] == '此用户没有填写评论!' || strlen($review['rateContent']) < 10 ) continue;

                    // 商城的一律只能给好评
                    $score = 4;
                    $reality = 4;
                    $service = 4;
                    $velocity = 4;
                    $rate = 4;
                    $product_id_list = $pid;
                    $product_id = $pid;
                    $content = $review['rateContent'];
                    $paymethod = "";
                    $feature = "";
                    $tag = "";
                    $view_count = 0;
                    $flower_count = 0;
                    $egg_count = 0;
                    $response_count = 0;
                    $pic_count = 0;
                    $product_count = 1;
                    $uploaded_files = "";
                    $reviewDAO->AddReview(1, $sid, GetCurrentDate(), $product_id_list,
                        $score, $reality, $service, $velocity, $rate, $content, $paymethod,
                        $feature, $tag, $view_count, $flower_count, $egg_count, $response_count,
                        $pic_count, $product_count, $uploaded_files, $product_id, $review['id'] );

                    //inset task to Message Queue
                    $taskMgr->AddProductScoreTask( $pid );
                    $taskMgr->AddShopScoreTask( $sid );
                    $data = array( "shop_id"=> $sid, "tag"=>"", "paymethod"=>"", "feature"=>"");
                    $taskMgr->AddShopInfoTask( $data );
                }
            } else {
                $errmsg->add('reg', '获取商品评价失败！');
                TransferTo('not_allow_add_review');
                return;
            }
        }
    } else {
        $errmsg->add('reg', '店铺信息无法获取，获取商品评价失败！');
        TransferTo('not_allow_add_review');
        return;
    }

    // when  everything is OK
    edp_redirect(edp_href_link(PAGENAME_PRODUCT_INFO, "pid=$pid"));
}
else{
    $errmsg->add('reg', '商品信息不完整，无法获取评价！');
    TransferTo('not_allow_add_review');
    return;
}

?>
